from django.db import models
from areas.models import Area
from goods.models import Goods, GoodsCategory, Brand, GoodsAttribute, DiscountPackage
from users.models import User, Address


class BaseModel(models.Model):
    """为模型类补充字段"""
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True  # 说明是抽象模型类, 用于继承使用，数据库迁移时不会创建BaseModel的表


class DiscountCoupon(BaseModel):
    """优惠券"""
    CATEGORY = {
        (1, "全场通用"),
        (2, "部分商品适用"),
        (3, "个人专享"),
    }
    name = models.CharField(max_length=50, verbose_name='优惠券名称')
    value = models.FloatField(default=0, verbose_name='价值')
    start_time = models.DateField(verbose_name='使用起始时间')
    end_time = models.DateField(verbose_name='使用截至时间')
    launch_counts = models.IntegerField(default=0, null=True, blank=True, verbose_name='发行量上限')
    issuance_counts = models.IntegerField(default=0, null=True, blank=True, verbose_name='已领取数量')
    used_counts = models.IntegerField(default=0, verbose_name='使用过数量')
    status = models.BooleanField(default=False, verbose_name='已经使用')
    is_show = models.BooleanField(default=True, verbose_name='向客户展示')
    full = models.IntegerField(default=0, verbose_name='满减值')
    category = models.IntegerField(choices=CATEGORY, default=1, verbose_name='优惠券分类')
    get_url = models.URLField(default=None,null=True, blank=True,verbose_name='领取路径')
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')

    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="用户id")
    used_time = models.DateTimeField(null=True, blank=True,verbose_name='用户使用时间')
    category_brand = models.ForeignKey(Brand, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="优惠券商品品牌")
    category_category = models.ForeignKey(GoodsCategory, on_delete=models.SET_NULL, null=True, blank=True,
                                          verbose_name="优惠券商品类别")

    class Meta:
        db_table = 'tb_discountcoupon'
        verbose_name = '优惠券'
        verbose_name_plural = verbose_name
        ordering = ['-update_time']

    def __str__(self):
        return self.name


class GoodsList(models.Model):
    discountcoupon_id = models.ForeignKey(DiscountCoupon, null=True, blank=True, on_delete=models.PROTECT,
                                          related_name='goods_list', verbose_name="优惠券id")
    cate_goods = models.ForeignKey(Goods, on_delete=models.SET_NULL,related_name="cate_goods", null=True, blank=True, verbose_name="优惠券商品")

    class Meta:
        db_table = 'tb_goodslist'
        verbose_name = '优惠券适用商品表'
        verbose_name_plural = verbose_name


class Courier_Company(BaseModel):
    """快递公司模型"""
    name = models.CharField(max_length=64, verbose_name="快递公司名称")
    price = models.FloatField(verbose_name="寄件单价")
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        db_table = "tb_Courier_company"
        verbose_name = '快递信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Business_Address(BaseModel):
    """商家退货地址管理"""
    name = models.CharField(max_length=20, verbose_name="收货地址名称")
    province = models.ForeignKey(Area, on_delete=models.PROTECT, related_name="province", verbose_name="省")
    city = models.ForeignKey(Area, on_delete=models.PROTECT, related_name="city", verbose_name="市")
    district = models.ForeignKey(Area, on_delete=models.PROTECT, related_name="district", verbose_name="县")
    place = models.CharField(max_length=128, verbose_name="详细地址")
    phone_number = models.CharField(max_length=11,  verbose_name="联系方式")
    person = models.CharField(max_length=11,  verbose_name="联系人")
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        db_table = "tb_business_address"
        verbose_name = '商家退货地址管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class OrderInfo(BaseModel):
    """客户订单"""
    PAY_METHODS_ENUM = {
        "CASH": 1,
        "ALIPAY": 2
    }

    PAY_METHOD_CHOICES = (
        (1, "微信支付"),
        (2, "货到付款"),
    )

    ORDER_STATUS_CHOICES = (
        (1, "待支付"),
        (2, "待发货"),
        (3, "待收货"),
        (5, "已完成"),
        (6, "已取消"),
    )
    # (8, "交易失败"),
    order_id = models.CharField(max_length=64, primary_key=True, verbose_name="商城订单号")
    transaction_id = models.CharField(max_length=64, null=True, blank=True, verbose_name="微信订单号")
    total_count = models.IntegerField(default=0, verbose_name="商品总数")
    total_amount = models.FloatField(default=0, verbose_name="商品总金额")
    freight = models.FloatField(default=0, null=True, blank=True, verbose_name="运费")
    pay_method = models.IntegerField(choices=PAY_METHOD_CHOICES, default=1, verbose_name="支付方式")
    status = models.IntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name="订单状态")
    post_script = models.CharField(max_length=200, null=True, blank=True, verbose_name="订单留言")
    numbers = models.CharField(max_length=64, null=True, blank=True, verbose_name="快递单号")
    is_show = models.BooleanField(default=True, verbose_name='向客户展示')
    send_time = models.DateTimeField(null=True, blank=True, verbose_name='发货时间')
    pay_time = models.DateTimeField(null=True, blank=True, verbose_name='付款时间')
    obtain_time = models.DateTimeField(null=True, blank=True, verbose_name='订单签收时间')
    success_time = models.DateTimeField(null=True, blank=True, verbose_name='用户确认收货时间')
    over_time = models.IntegerField(default=0, verbose_name="延长收货时间")
    discountcoupon = models.ForeignKey(DiscountCoupon, default=None, on_delete=models.PROTECT, null=True, blank=True,verbose_name="抵扣券")
    discountpackage = models.ForeignKey(DiscountPackage, default=None, on_delete=models.PROTECT, null=True, blank=True,verbose_name="优惠套餐")
    discount_value = models.FloatField(default=None,null=True, blank=True,verbose_name="优惠金额")
    user = models.ForeignKey(User, null=True, blank=True, on_delete=models.PROTECT, verbose_name="下单用户")
    address = models.ForeignKey(Address, null=True, blank=True, on_delete=models.PROTECT, verbose_name="收获地址")
    courier_company = models.ForeignKey(Courier_Company, null=True, blank=True, on_delete=models.PROTECT,
                                        verbose_name="发货快递")
    business_address = models.ForeignKey(Business_Address, default=1, null=True, blank=True, on_delete=models.PROTECT,
                                         verbose_name="默认维修收货地址")
    check_script = models.CharField(max_length=200, null=True, blank=True, verbose_name="客服留言")
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')


    class Meta:
        db_table = "tb_order_info"
        verbose_name = '订单基本信息'
        verbose_name_plural = verbose_name


class OrderGoods(BaseModel):
    """
    订单包含商品
    """
    SCORE_CHOICES = (  # 满意度评分
        (0, '0分'),
        (1, '20分'),
        (2, '40分'),
        (3, '60分'),
        (4, '80分'),
        (5, '100分'),
    )
    RETURN = (
        (0, "未发生退换"),
        (1, "退货中"),  # 客户退货申请
        (2, "退货成功"),
        (3, "换货中"),  # 客户换货申请
        (4, "换货成功"),
        (5, "维修中"),  # 客户申请维修
        (6, "维修成功"),
        (7, "仅退款"),  # 未发货状态下申请退款
        (8, "退款成功"),
    )
    order = models.ForeignKey(OrderInfo, related_name='goods', on_delete=models.CASCADE, verbose_name="订单")
    goods = models.ForeignKey(GoodsAttribute, on_delete=models.PROTECT, verbose_name="订单商品")
    count = models.IntegerField(default=0, verbose_name="数量")
    price = models.FloatField(default=0, verbose_name="单价")
    comment = models.TextField(null=True, blank=True, verbose_name="评价信息")
    score = models.SmallIntegerField(choices=SCORE_CHOICES, default=5, verbose_name='满意度评分')
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    is_commented = models.BooleanField(default=False, verbose_name='是否评价了')
    is_return = models.IntegerField(choices=RETURN, default=0, verbose_name="商品退换状态")

    class Meta:
        db_table = "tb_order_goods"
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name


class Reason(BaseModel):
    """退换货原因"""
    RETURN_TYPE_CHOICE = (
        (0, "未付款"),
        (1, "已付款未发货"),
        (2, "待收货"),
    )
    explain = models.CharField(max_length=100, verbose_name='原因')
    is_enabled = models.BooleanField(default=False, verbose_name='是否可用')
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')
    return_type = models.IntegerField(choices=RETURN_TYPE_CHOICE, verbose_name='退换货类型')

    class Meta:
        db_table = "tb_reason"
        verbose_name = '退换货原因'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.explain


class ReturnDetail(BaseModel):
    """退换商品细节表格"""
    CHECK_RESULT = (
        (0, "商家未处理"),
        (1, "商家已拒绝退货"),
        (2, "商家已同意退货"),
        (3, "商家已拒绝换货"),
        (4, "商家已同意换货"),
        (5, "商家已拒绝退款"),
        (6, "商家已同意退款"),
        (7, "商家已拒绝维修"),
        (8, "商家已同意维修"),
        (9, "退货途中"),
        (10, "维修途中"),
        (11, "换货途中"),
        (12, "售后完成")
    )

    user = models.ForeignKey(User, null=True, blank=True,related_name= "customer" , on_delete=models.CASCADE, verbose_name="申请退换用户")
    return_numbers = models.CharField(max_length=64, default=0, verbose_name="退换货编号")
    numbers = models.CharField(max_length=64, null=True, blank=True, verbose_name="退换快递单号")
    company = models.CharField(max_length=64, null=True, blank=True, verbose_name="退换快递名称")
    apply_time = models.DateTimeField(null=True, blank=True, verbose_name='申请时间')
    check_time = models.DateTimeField(null=True, blank=True, verbose_name='审核处理时间')
    check_user = models.ForeignKey(User, null=True, blank=True,related_name= "check_user", on_delete=models.CASCADE, verbose_name="审核处理人员")
    check_results = models.IntegerField(choices=CHECK_RESULT, default=0, verbose_name="审核处理结果")
    check_reason = models.CharField(max_length=200, null=True, blank=True, verbose_name="审核处理理由")
    reason = models.ForeignKey(Reason, on_delete=models.CASCADE, verbose_name='退换货原因')
    introduce = models.CharField(max_length=200, null=True, blank=True, verbose_name="退换原因说明")
    return_id = models.IntegerField(null=True, blank=True, verbose_name="需要更换的商品属性id")
    return_address_id = models.IntegerField(null=True, blank=True, verbose_name="需要更换收货地址id")
    return_money = models.FloatField(null=True, blank=True, verbose_name="退款金额")
    order_goods_id = models.ForeignKey(OrderGoods, related_name='return_reason', on_delete=models.PROTECT,
                                       verbose_name="退货换货详情")
    is_show = models.BooleanField(default=True, verbose_name='向客户展示')


    class Meta:
        db_table = "tb_return_detail"
        verbose_name = '退换商品详情'
        verbose_name_plural = verbose_name


class OrderInfoUpdate(models.Model):
    orderinfo = models.ForeignKey(OrderInfo, related_name='orderinfoupdate',on_delete=models.CASCADE, verbose_name='关联订单编号')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    check_user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="审核处理人员")
    new = models.BooleanField(default=True,verbose_name="是否是最新的数据")

    old_receiver = models.CharField(max_length=20, null=True, blank=True, verbose_name='更新前的收货人',)
    update_receiver = models.CharField(max_length=20, null=True, blank=True, verbose_name='更新的收货人',)
    old_address = models.CharField(max_length=20, null=True, blank=True, verbose_name='更新前的收货地址',)
    update_address = models.CharField(max_length=20, null=True, blank=True, verbose_name='更新的收货地址',)
    old_mobile = models.CharField(max_length=20, null=True, blank=True, verbose_name='更新前的收货人联系方式',)
    update_mobile = models.CharField(max_length=20, null=True, blank=True, verbose_name='更新的收货人联系方式',)
    old_total_amount = models.FloatField( null=True, blank=True, verbose_name='更新前的订单总金额',)
    update_total_amount = models.FloatField( null=True, blank=True, verbose_name='更新的订单总金额',)
    old_freight = models.FloatField( null=True, blank=True, verbose_name='更新前的总邮寄费用',)
    update_freight = models.FloatField( null=True, blank=True, verbose_name='更新后的邮寄费用',)
    class Meta:
        db_table = "tb_orderinfoupdate"
        verbose_name = '订单修改表'
        verbose_name_plural = verbose_name


class ReturnDetailImage(BaseModel):
    """退换商品细节图片"""
    image = models.ImageField(upload_to='goods/attribute/images', null=True, blank=True, verbose_name="退换图片")
    return_detail = models.ForeignKey(ReturnDetail, related_name='return_image', on_delete=models.CASCADE,
                                      verbose_name="退换原因")

    class Meta:
        db_table = "tb_return_image"
        verbose_name = '退换商品图片'
        verbose_name_plural = verbose_name

